package com.car.service.Impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.config.AlipayConfig;
import com.car.entity.Carrentalorderform;
import com.car.entity.Detailedlistofrentalcharges;
import com.car.mapper.CarrentalorderformMapper;
import com.car.mapper.DetailedlistofrentalchargesMapper;
import com.car.page.Page;
import com.car.service.CarrentalorderformService;
import com.car.util.SUKUtil;
import com.car.vo.CarrentalorderformVo;
import com.car.vo.DetailedlistofrentalchargesVo;
import com.car.vo.VehiclemotioncarVo;
import com.car.vo.orderVo;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CarrentalorderformServiceImpl implements CarrentalorderformService{
	
	@Autowired
	CarrentalorderformMapper carrentalorderformMapper;
	@Autowired
	DetailedlistofrentalchargesMapper detailedlistofrentalchargesMapper;

	@Override
	public int deleteByPrimaryKey(Integer orderId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(Carrentalorderform record,VehiclemotioncarVo veh,Detailedlistofrentalcharges detailedlistofrentalcharges) {
		// TODO Auto-generated method stub
		record.setOrderId(SUKUtil.getRandom());
		record.setHitchingrideId(veh.getHitchingrideId());
		record.setLimitTheTotalMileage(String.valueOf(veh.getLimitedRange()));
		record.setTheCarStores(veh.getThecarstores()); //驅車門店
		record.setTakeCarcity(veh.getTakeCarCity()); //驅車地點
		record.setCarCity(veh.getCarCity()); //還車地點
		record.setOrderTypes(1);
		int nums = carrentalorderformMapper.insert(record);
		if(nums>0){
			detailedlistofrentalcharges.setOrderId(record.getOrderId());
			detailedlistofrentalcharges.setPayInAdvance(0);
			detailedlistofrentalcharges.setVehicleCost(veh.getTheRent()/2);
			detailedlistofrentalcharges.setTotal(veh.getTheRent()/2*veh.getDays());
			detailedlistofrentalcharges.setOtherFees(0);
			detailedlistofrentalcharges.setBasicServiceCharge(0);
			detailedlistofrentalcharges.setHonourService(0);
			detailedlistofrentalcharges.setRemoteCarserver(0);
			nums = detailedlistofrentalchargesMapper.insert(detailedlistofrentalcharges);
			if(nums>0){
				return nums;
			}
		}
		return 0;
				
				
	}

	@Override
	public Carrentalorderform selectByPrimaryKey(Integer orderId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Carrentalorderform> selectAll() {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.selectAll();
	}

	@Override
	public int updateByPrimaryKey(Carrentalorderform record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Page<CarrentalorderformVo> selectAllbyuserId(Integer userId,Integer orderState,Integer pageNo, Integer pageSize) {
		Page<CarrentalorderformVo> page = new Page<CarrentalorderformVo>(pageNo,pageSize);
		List<CarrentalorderformVo> lists = carrentalorderformMapper.selectAllbyuserId(userId,orderState,(pageNo-1)*page.getPageSize(), page.getPageSize());
	
			page.setTotalRow(carrentalorderformMapper.selectAllbyuserIdToCount(userId, orderState));
		
		
		page.setList(lists);
		// TODO Auto-generated method stub
		return page;
	}

	@Override
	public int selectAllbyuserIdToCount(Integer userId, Integer orderState) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.selectAllbyuserIdToCount(userId, orderState);
	}

	@Override
	public CarrentalorderformVo selectAllToInfos(String orderId,Integer i) {
		// TODO Auto-generated method stub
		if(i>0){
			return carrentalorderformMapper.selectAllToInfosShun(orderId);
		}
		return carrentalorderformMapper.selectAllToInfos(orderId);
	}

	@Override
	public int updOrderState(String orderState, String orderId) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.updOrderState(orderState, orderId);
	}

	@Override
	public int selectAllbyuserIdToCountShun(Integer userId, Integer orderState) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.selectAllbyuserIdToCountShun(userId, orderState);
	}

	@Override
	public Page<CarrentalorderformVo> selectAllbyuserIdTwo(Integer userId, Integer orderState, Integer pageNo,
			Integer pageSize) {
		Page<CarrentalorderformVo> page = new Page<CarrentalorderformVo>(pageNo,pageSize);
		List<CarrentalorderformVo> lists = carrentalorderformMapper.selectAllbyuserIdTwo(userId,orderState,(pageNo-1)*page.getPageSize(), page.getPageSize());
	
		page.setTotalRow(carrentalorderformMapper.selectAllbyuserIdToCountShun(userId, orderState));
		
		
		page.setList(lists);
		// TODO Auto-generated method stub
		return page;
	}

	@Override
	public void ali(HttpServletResponse response, HttpServletRequest request,String orderId,Integer money) throws AlipayApiException, IOException {
		// TODO Auto-generated method stub
		//设置编码
        response.setContentType("text/html;charset=utf-8");
 
        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        //设置请求参数
        AlipayTradePagePayRequest aliPayRequest = new AlipayTradePagePayRequest();
        aliPayRequest.setReturnUrl(AlipayConfig.return_url);
        aliPayRequest.setNotifyUrl(AlipayConfig.notify_url);
        //商户订单号，后台可以写一个工具类生成一个订单号，必填
        String order_number = orderId;
        //付款金额，从前台获取，必填
        String total_amount = money.toString();
        //订单名称，必填
        String subject = new String("顺风车订单");
        aliPayRequest.setBizContent("{\"out_trade_no\":\"" + order_number + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = alipayClient.pageExecute(aliPayRequest).getBody();
        //输出
        out.println(result);//以下写自己的订单代码
		
	}

	@Override
	public Carrentalorderform selectByOrderIdTolistener(String orderId) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.selectByOrderIdTolistener(orderId);
	}

	@Override
	public int addOrder(orderVo orders,DetailedlistofrentalchargesVo details,CarrentalorderformVo order) {
		// TODO Auto-generated method stub
		SUKUtil util = new SUKUtil();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date qctime;
		Date hctime;
		try {
			qctime = sdf.parse(orders.getTime().get("qctime").toString());
			hctime = sdf.parse(orders.getTime().get("hctime").toString());
			order.setOrderId(util.getRandom());
			order.setUserId(orders.getUser().getUserId());
			order.setCarId(orders.getCar().getCarId());
			order.setVehicleTakingTime(qctime);
			order.setReturnTime(hctime);
			order.setOrderDate(new Date());
			order.setOrderState("7");
			if(orders.getScsm().equals("1")){
				order.setDoorTo(1);
				order.setPickUpDetail(orders.getQccity());
			}
			else{
				order.setDoorTo(2);
				order.setTheCarStores(orders.getQccity());
			}
			if(orders.getQcsm().equals("1")){
				order.setDoorPicking(1);
				order.setReturnDetail(orders.getHccity());
			}
			else{
				order.setDoorPicking(2);
				order.setTheDoorPoint(orders.getHccity());
			}
			
			details.setOrderId(order.getOrderId());
			details.setVehicleCost((int)orders.getCar().getTheRent());
			details.setBasicServiceCharge(40);
			details.setOtherFees(35);
			int f = detailedlistofrentalchargesMapper.addDetail(details);
			if(f!=0){
				return carrentalorderformMapper.addOrder(order);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
	public List<Carrentalorderform> selectAlls() {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.selectAlls();
	}

	@Override
	public int selectCount() {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.selectCount();
	}

	@Override
	public Page<Carrentalorderform> selectOderPageList(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		Page<Carrentalorderform> pageInfo= new Page<Carrentalorderform>(pageNo,pageSize);
		pageInfo.setCurrentPage(pageNo);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotalRow(carrentalorderformMapper.selectCount());
		List<Carrentalorderform> list =carrentalorderformMapper.selectOderPageList((pageNo-1)*pageSize, pageSize);
		pageInfo.setList(list);
		return pageInfo;
	}

	@Override
	public Carrentalorderform selectOderId(String orderId) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.selectOderId(orderId);
	}

	@Override
	public int updateOrderState(String orderId ,String limitTheTotalMileage) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.updateOrderState(orderId,limitTheTotalMileage);
	}

	@Override
	public int shunfengOrderCount() {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.shunfengOrderCount();
	}

	@Override
	public Page<Carrentalorderform> shenfengPageLsit(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		Page<Carrentalorderform> pageInfo= new Page<Carrentalorderform>(pageNo,pageSize);
		pageInfo.setCurrentPage(pageNo);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotalRow(carrentalorderformMapper.shunfengOrderCount());
		List<Carrentalorderform> list =carrentalorderformMapper.shenfengPageLsit((pageNo-1)*pageSize, pageSize);
		pageInfo.setList(list);
		return pageInfo;
	}

	@Override
	public Carrentalorderform shunfengSelectOrderId(String orderId) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.shunfengSelectOrderId(orderId);
	}
@Override
	public Carrentalorderform selectOrderIdcha(String id) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.selectOrderIdcha(id);
	}

	@Override
	public Carrentalorderform shunfengOrderIdcha(String id) {
		// TODO Auto-generated method stub
		return carrentalorderformMapper.shunfengOrderIdcha(id);
	}
@Override
	public Page<Carrentalorderform> xingCheng(Integer carId, Integer start, Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		Page<Carrentalorderform>pageInfo = new Page<Carrentalorderform>(pageNo, pageSize);
		pageInfo.setCurrentPage(pageNo);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotalRow(0);//
		List<Carrentalorderform>list = carrentalorderformMapper.xingCheng(carId, start, (pageNo-1)*pageSize, pageSize);
		pageInfo.setList(list);
		return pageInfo;
	}
}